#include "dbtabdatabaseinfo.h"
#include "databasequery.h"
#include <QDebug>
const QString DBTabDataBaseInfo::TAB_NAME = "db_info";
const QString DBTabDataBaseInfo::_INFO_NAME = "name";
const QString DBTabDataBaseInfo::_INFO_VALUE = "value";
const QString DBTabDataBaseInfo::_UPDATE_TIME = "datetime";

void DBTabDataBaseInfo::createTab(QSqlDatabase &db)
{
    DataBaseQuery query(db);
   // query.exec("drop table " + TAB_NAME);
    QString str = "CREATE TABLE "+TAB_NAME +"(\n" +
            _ID + " integer primary key "AUTO_INCREMENT",-- \n" +
            _INFO_NAME + " varchar(64) NOT NULL,-- 信息名称\n" +
            _INFO_VALUE + " varchar(64),-- 信息值\n" +
            _UPDATE_TIME + " datetime -- 更新时间\n" +
            ") -- 数据库信息表\n";
    qDebug()<<str;
    if(query.exec(str)==false)
    {
         qDebug()<<" create " +  TAB_NAME + " error ";
    }
}

void DBTabDataBaseInfo::setValue(QSqlDatabase &db,const QString &name,const QString &value)
{
    DataBaseQuery query(db);
    QMap<QString,QVariant> map;
    map.insert(_INFO_VALUE,value);
    map.insert(_INFO_NAME,name);
    map.insert(_UPDATE_TIME,QDateTime::currentDateTime());
    qDebug()<<QDateTime::currentDateTime();
    query.insert(TAB_NAME,map);

}

QString DBTabDataBaseInfo::getValue(QSqlDatabase &db,const QString &name)
{
    DataBaseQuery query(db);
    QString str = QString("select max(")+_ID+"),"+_INFO_VALUE + " from "+ TAB_NAME +" where " +  _INFO_NAME + " = '" +name+ "'";
    query.exec(str);

    if(query.first())
    {
        qDebug()<<query.value(0)<<query.value(1);
        return query.value(1).toString();
    }
    return "";

}

